package cn.onlov.video.controller;

import cn.onlov.video.bo.FfmpegBo;
import cn.onlov.video.bus.Result;
import cn.onlov.video.bus.ResultUtil;
import cn.onlov.video.util.MediaUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.sql.Time;

@RestController
@Slf4j
public class IndexController {

@RequestMapping("/")
public String index(){



    String input = "D:\\vod\\222.MOV";
    String outPuth  = "D:\\vod\\22.jpg";
//    MediaUtil.cutVideoFrame(new File(input),new File(outPuth) ,new Time(0,0,4),1080,788);
    return "success";
}

    /**
     *
     * @param bo
     * @return
     */
    @RequestMapping("/video/deal")
    public Result<String> deal(@RequestBody FfmpegBo bo) {

        Result<String> result = new Result<>();
        log.info("调用转换功能" + bo.toString());
        String s = checkBo(bo);
        if(!StringUtils.isEmpty(s)){
            return ResultUtil.error(s);
        }
        if (null == bo || null == bo.getConvertType()) {
            return ResultUtil.error( "请求转换入参为空");
        }
        switch (bo.getConvertType()) {
        case 1: // 裁剪
            result = cutVideo(bo);  // 已完成
            break;
        case 2:  // 带图片的裁剪
            result = cutWithPic(bo);
            break;
        case 3: // 压缩
            result = zipVideo(bo);

            break;
        case 4: // 转码
            result = convertVideo(bo);
            break;
        }
        return result;

    }


    private Result<String> cutVideo(FfmpegBo bo) {
        String sourcePath = bo.getSourcePath();
        String targerPath = bo.getTargerPath();
        String cutStartTime = bo.getCutStartTime();
        Long cutLong = bo.getCutLong();
        Result<String> result = new Result<>();
        try {
            result = MediaUtil.cutVideo(sourcePath, targerPath, cutStartTime, cutLong);
            return ResultUtil.success();
        }catch (Exception e){
            log.error("转换失败" + e);
        }
        return ResultUtil.success(result);

    }

    /**
     * 带图片的裁剪
     */

    public Result<String> cutWithPic(FfmpegBo bo) {
        return ResultUtil.success();
    }


    /**
     * 压缩视屏
     */
    private Result<String> zipVideo(FfmpegBo bo) {
        return ResultUtil.success();
    }

    private Result<String> convertVideo(FfmpegBo bo) {
        Result<String> result = MediaUtil.convert2Video(bo.getSourcePath(), bo.getTargerPath() ,bo.getFrame());
        return result;
    }


    /**
     * 校验入参
     * @param bo
     * @return
     */
    private String checkBo(FfmpegBo bo) {
        StringBuilder builder = new StringBuilder();
        if (null == bo.getSourcePath() || null == bo.getTargerPath()) {
            builder.append("-源文件路径为传值或目标文件未传值");
            return builder.toString();
        }
        File file = new File(bo.getSourcePath());
        if (!file.exists()) {
            builder.append(bo.getSourcePath() + "-源文件在对应路径未找到");
            return builder.toString();
        }
        if( !StringUtils.isEmpty(bo.getTargerPath())  && new File(bo.getTargerPath()).exists()){
            builder.append(bo.getSourcePath() + "-对应路径已有重名文件");
            return builder.toString();
        }
        switch (bo.getConvertType()) {
        case 1: // 裁剪
            if (null == bo.getCutStartTime() || null == bo.getCutLong()) {
                builder.append(bo.getSourcePath() + "没有传递裁剪的开始时间和裁剪的时长，");
            }
            if (null == bo.getTargerPath()) {
                builder.append( "转换后的视频路径为空，请检查转换后的视频存放路径是否正确");
            }
            break;
        case 2:  // 带图片的裁剪
            if (null == bo.getCutStartTime() || null == bo.getCutLong()) {
                builder.append(bo.getSourcePath() + "没有传递裁剪的开始时间和裁剪的时长，");
            }
            if(null == bo.getFrame()){
                builder.append( "没有传递裁剪的开始时间和裁剪的时长");
            }
            break;
        case 3: // 压缩
            break;
        case 4: // 转码
            if(null==bo.getFrame()){
                builder.append( "没有传递裁剪的开始时间和裁剪的时长");
            }
            break;
        }
        return builder.toString();
    }

}
